package com.crm.service.activity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.model.activity.dtos.ActivityDto;
import com.crm.model.activity.dtos.PageActivityDto;
import com.crm.model.activity.pojos.Activity;
import com.crm.model.common.dtos.PageResponseResult;
import com.crm.model.common.dtos.ResponseResult;
import com.crm.model.common.enums.AppHttpCodeEnum;
import com.crm.service.activity.mapper.ActivityMapper;
import com.crm.service.activity.service.ActivityService;
import com.crm.common.constant.NumberConstant;
import com.crm.utils.NumbersUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;

    /**
     * 活动管理分页查询
     *
     * @param dto
     * @return
     */
    public ResponseResult findActivityIdOrChannelOrCreatedTimeOrEndTime(PageActivityDto dto) {
        //参数校验
        dto.checkParam();
        //1.构造查询条件
        LambdaQueryWrapper<Activity> activityWrapper = new LambdaQueryWrapper<>();
        IPage<Activity> page = new Page(dto.getPage(), dto.getSize());
        if (dto.getActivityId() != null) {
            activityWrapper.eq(Activity::getActivityId, dto.getActivityId());
        }
        if (dto.getChannel() != null) {
            activityWrapper.eq(Activity::getChannel, dto.getChannel());
        }
        if (dto.getCreatedTime() != null && dto.getEndTime() != null) {
            activityWrapper.between(Activity::getCreatedTime, dto.getCreatedTime(), dto.getEndTime());
        }
        //分页查询
        page = page(page, activityWrapper);
        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    /**
     * 创建活动
     *
     * @param dto
     * @return
     */
    public ResponseResult addActivity(ActivityDto dto) {
        //调用参数校验方法
        ResponseResult result = checkParameters(dto);
        if (result != null) {
            return result;
        }
        //创建活动
        Activity activity = new Activity();
        //编号生成
        String activityId = getString();
        //存入编号
        activity.setActivityId(activityId);
        BeanUtils.copyProperties(dto, activity);
        activityMapper.insert(activity);
        return ResponseResult.okResult(null);
    }

    /**
     * 编号重复度校验
     * @return
     */
    private String getString() {
        String activityId = NumbersUtils.GeneratingNumbers(NumberConstant.ACTIVITY_HEAD, NumberConstant.ACTIVITY_LENGTH);
        //判断编号是否重复
        LambdaQueryWrapper<Activity> activityWrapper = new LambdaQueryWrapper<>();
        activityWrapper.eq(Activity::getActivityId,activityId);
        Activity activity1 = activityMapper.selectOne(activityWrapper);
        if (activity1 != null) {
            activityId = getString();
        }
        return activityId;
    }


    /**
     * 生成编号
     * @return
     */

    /**
     * 创建活动参数校验
     *
     * @param dto
     * @return
     */
    private ResponseResult checkParameters(ActivityDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if (dto.getChannel() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (StringUtils.isBlank(dto.getProfile())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (StringUtils.isBlank(dto.getType())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (dto.getCreatedTime() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (dto.getEndTime() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        return null;
    }
}
